if not pattern then pattern = {} end

if not pattern.jet then

pattern.jet = {last_time = {}}

if not ScriptsRootPath then
	ScriptsRootPath = getScriptPath() .. "\\..\\"
end

pattern.jet.patterns_init = lib.path.init .. "patterns.ini"

dofile (ScriptsRootPath .. "lib\\lib.lua")

function pattern.jet.Init()
	pattern.jet.params = lib:LoadIni(pattern.jet.patterns_init, "jet", {balance = 0.02, jet = 0.2})
	if processor and processor.log then
		processor:log("pattern.jet.params", pattern.jet.params)
	end
end

function pattern.jet.OnCandle(source, index, data)
	if not source then return end
	if not index then index = 1 end
	if not pattern.jet.params then pattern.jet.Init() end
	
	local after = {
		lib:GetLastCandle(source, index + 1),
		lib:GetLastCandle(source, index)
	}
	local candle = lib:GetLastCandle(source, index + #after)
	local candle1 = lib:GetLastCandle(source, index + #after + 1)
	
	if not (candle and candle1 and candle.instrument) then return end
	
	local body = math.abs(candle.close - candle.open)
	if candle.high - candle.low - body <= candle.instrument.min_price_step then return end
	
	body = body * 100 / candle.close
	if body > pattern.jet.params.balance then return end
	
	local jet = (after[#after].close - candle.close) * 100 / candle.close
	
	--for i = 2, #after do
	--	jet = math.max(jet, math.abs(after[i].close - candle.close) * 100 / candle.close)
	--end
	if math.abs(jet) < pattern.jet.params.jet then return end
	
	if not pattern.jet.last_time[candle.instrument.code] then
		pattern.jet.last_time[candle.instrument.code] = {}
	end
	
	pattern.jet.last_time[candle.instrument.code][lib:tickseconds(candle.datetime)] = jet
	
	if pattern.jet.last_time[candle.instrument.code][lib:tickseconds(candle1.datetime)] then return end
	
	local is_long = jet > 0
	
	local price = lib:NormalizePrice(candle.close, candle.instrument, is_long)
	
	candle.datetime.is_long__ = is_long
	candle.datetime.hint__ = lib:FormatPrice(price, candle.instrument)
			
	candle.datetime.hint__ = string.format("JET %s %s%%",
		lib:FormatPrice(price, candle.instrument),
		tostring(round(jet, 2))
	)
			
	candle.datetime.notify__ = candle.instrument.code .. " " .. candle.datetime.hint__
	
	return price, candle.datetime
end

end -- of file
